<?

/*	TagCloud OBJECT
*	Methods:
*	output
*
*
*					*/


class TagCloud extends MOD {

	function __construct( $vars ) {

		$this->_addvars( $vars );
		$this->_ref();
	}
	
	
	public function output() {
		
		if( $this->action )
			$result = $this->{ "filter_" . $this->action }();

		if( $result )
			$this->addclass( $this->action );
		
		if( is_array( $result ))
			$result = implode( " ", $result );
	
		return $this->parse( $result ) . $this->render_js();
	
	}
	
	public function filter_basic() {
		global $DB;
		
		if( !$condt = $this->vars->condt )
			 $condt = "active order by rank desc, flog desc limit " . $this->vars->limit;
		
		if( $cloud = $DB->filter_objects( $this->vars->type, $condt ))
			
			foreach ( $cloud as $Tag ) $tags[] = $this->render_tag( $Tag );


		return $tags;
	}
	
	public function filter_object() {
		global $DB, $Object;
		
		if( $this->vars->id )
			$Object = new OBJECT( $this->vars->type, $this->vars->id );
		
		$tags = $this->vars->include_related ? 
			$Object->tags->filter_referenced() :
			$Object->tags->filter();
		
		if( $tags )
			foreach( $tags as $Tag )
				$result[] = $Tag->render( true, $this->vars->class );
		
		return $result;
	}
	
	private function filter_popular() {
		global $DB;
		
		$lines[] = "<ul>";
		
		if( $cloud = $DB->reference( "location", "active = 1 group by objid order by count(*) desc limit {$this->vars->limit}", "tag" , false, "tag" ))
			foreach( $cloud as $tid ) {
				
				$Tag 		= new TAG(( int ) $tid );
				$lines[] 	= "<li><a href='{$Tag->uri()}' ". ( $Tag->hasContent()? "title='{$Tag->get('content')}'": "" ). ">{$Tag->get('name')}</a></li>";
			}
		
		$lines[] = "<li class='stable'></li>";
		$lines[] = "</ul>";
		
		return $lines;
		/*if( !$this->vars->limit ) $this->vars->limit = 24; 
		$long_key = ( $this->vars->maxchars )? "concat( SUBSTRING( <%lang:contid:long%>, 1, {$this->vars->maxchars}), ' ...' )" : "<%lang:contid:long%>";
		
		$nodes = $DB->filter_key(
			"news",
			"active && 	date < now() order by date desc limit " . $this->vars->limit,
			array( "ID", "creator", "date", "date_format( date, '{$this->vars->dformat}') as dformat", "<%lang:nameid%> as name", "$long_key as content", "(select user from user where ID=creator) as user" ) );
		
		if( $nodes ) 
			foreach($nodes as $nkey => $node ) $nodes[ $nkey ] = $this->render_node( $node, (( $href = $this->vars->href )? $href : false )); 

		return $nodes;*/
	}


	private function filter_fromChildren() {
		global $DB, $Children;
		
		if( !$Children ) return "create object array";
		
		foreach( $Children as $Child )
			$ids[] = $Child->id;
		
		$lines[] = "<ul>";
		
		if( $cloud = $DB->reference( "location", "ID in(". implode( $ids, "," ) .") group by objid order by count(*) desc limit {$this->vars->limit}", "tag" , false, "tag" ))
			foreach( $cloud as $tid ) {
				
				$Tag 		= new TAG(( int ) $tid );
				$lines[] 	= "<li><a href='{$Tag->uri()}' ". ( $Tag->hasContent()? "title='{$Tag->get('content')}'": "" ). ">{$Tag->get('name')}</a></li>";
			}
		
		$lines[] = "<li class='stable'></li>";
		$lines[] = "</ul>";
		
		return $lines;
	}

	private function filter_groupedChildren() {
		global $DB, $Children;
		
		if( !$Children ) return "create object array";

		
		foreach( $Children as $Child ) {
			
			$ids[] = $Child->id;

			if( $tags = $Child->ref_objects( "tag", 1, 124, "tag" ))
				foreach( $tags as $Tag )
					$list[ $Tag->id ][] = "<a href='{$Child->uri()}'>{$Child->get( "name" )}</a>";
		}
		
		
		
		$lines[] = "<ul>";
		
		if( $cloud = $DB->reference( "location", "ID in(". implode( $ids, "," ) .") group by objid order by count(*), rank desc limit {$this->vars->limit}", "tag" , false, "tag" ))
			foreach( $cloud as $tid ) {
				
				$Tag 		= new TAG( $tid = ( int ) $tid );
				$lines[] 	= "<li><a href='{$Tag->uri()}' ". ( $Tag->hasContent()? "title='{$Tag->get('content')}'": "" ). ">{$Tag->get('name')}</a> (" . count( $list[ $tid ]) . ")</li>";
			}
		
		$lines[] = "<li class='stable'></li>";
		$lines[] = "</ul>";
		
		return $lines;
	}

	
	public function filter_childSelect() {
		global $DB;

		$children = ( $this->vars->children )? $this->vars->children :
			$DB->reference( $this->vars->parenttype, $this->vars->parentid, $this->vars->objtype );
		
		$result[] = translate( "Uw selectie" ). ":<br /><select><option disabled='disabled'>".translate( "willekeurig..." )."</option>";
		if( $children ) foreach( $children as $child ) {
			$Child = new OBJECT( $this->vars->objtype, (int) $child );
			$selected = ( $Child->id == (int) $this->vars->selected )? " selected='selected'" : '';
			$result[] = "<option$selected value='{$Child->id}'>{$Child->get( 'name' )}</option>";
		}
		
		$result[] = "</select>";
		
		foreach( $DB->tags->random( 12 ) as $Tag )
			$result[] = $Tag->render( true, "rand" );
		
		if( $this->textid ) $result[] = "<hr />";
		
		$this->js = "mod = new TagCloud( 'childSelect', ". json_encode( $this->vars ) .", {$this->modid} )";
		
		return $result;
	}

	public function filter_random() {
		global $DB;
		
		if( $tags = $DB->tags->random( $this->vars->limit ))
			foreach( $tags as $Tag ) $result .= $Tag->get( "name" );
		
		return $result;
	}
	
	public function ajax_childSelect() {
		global $DB;
		
		//if( $children = $DB->tags->deep( $this->vars->objtype, (int) $this->vars->selected ))
		//	foreach( $children as $Tag )
		//		$result[] = array( "name"=> $Tag->get( "name" ), "uri"=> $Tag->uri());
		
		return json_encode( array( "tags" => $result ));
	}
	
	private function render_tag( $Tag )	{
	
			return "<a href='{$Tag->uri()}' title='tag: {$Tag->get( 'name' )}'>{$Tag->get( 'name' )}</a>";
	}
} 








?>